#include "databasecreator.h"

#include <QString>
#include <QSqlQueryModel>
#include <QLatin1String>

#define CREATE_AQFornecedor \
    "(ID_Fornecedor INTEGER PRIMARY KEY NOT NULL, " \
    "Nome VARCHAR(100), " \
    "Endereco VARCHAR(100), " \
    "Bairro VARCHAR(100), " \
    "PosLatitude VARCHAR(30), " \
    "PosLongitude VARCHAR(30), " \
    "Contato VARCHAR(30), " \
    "Tel1 VARCHAR(20), " \
    "Tel2 VARCHAR(20), " \
    "OBS TEXT) "


#define CREATE_P(nome) {#nome, "create table " #nome CREATE_##nome}
QString createScripts[1][2] =
{
    CREATE_P(AQFornecedor)
};

DatabaseCreator::DatabaseCreator(QObject *parent) :
    QObject(parent)
{
}

QSqlError DatabaseCreator::createDatabase()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

    QString dbPath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
    db.setDatabaseName(dbPath + QDir::separator() + "precolider.sqlite");

    if (!db.open())
        return db.lastError();

    for(int i=0; i<1; i++) {
        QString nome = createScripts[i][0];
        QString sql = createScripts[i][1];

        qDebug("Criando tabela: ");
        qDebug(nome.toUtf8());
        qDebug(sql.toUtf8());

        QSqlQuery query;
        if (!query.exec(sql))
            return query.lastError();
    }


    qDebug("created!");
    return QSqlError();
}

